#pragma once
#include <iostream>
#include <string>
#include <stdarg.h>
#include <cstdio>
#include <ctime>
//日志是由日志级别的
#define DEBUG    0//调试信息
#define NORMAL   1//正常
#define WARNING  2//警告
#define ERROR    3//错误
#define FATAL    4//致命

#define LOGFILE "./calculator.log"
const char *gLevelMap[] = {
    "DEBUG",
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};
//完整的日志功能：至少：日志等级、时间、日志内容、支持用户自定义
void logMessage(int level, const char* format, ...) //...代表可变参数
{   
#ifndef DEBUG_SHOW
    if(level == DEBUG) return;
#endif
    char stdBuffer[1024]; //标准部分
    struct tm  *tp;  
    time_t t = time(NULL);  
    tp = localtime(&t);

    snprintf(stdBuffer, sizeof stdBuffer, "[%s] [%d/%d/%d-%d:%d:%d] ", gLevelMap[level], tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday,\
    tp->tm_hour,tp->tm_min,tp->tm_sec);

    char logBuffer[1024]; 
    va_list ap;
    va_start(ap,format);
    // vprintf(format,ap);
    vsnprintf(logBuffer,sizeof logBuffer, format,ap);
    va_end(ap); //ap=nullptr


    // printf("%s%s\n", stdBuffer, logBuffer);
    FILE*fp = fopen(LOGFILE,"a");
    fprintf(fp,"%s%s\n",stdBuffer,logBuffer);
    fclose(fp);
}

